package com.nwpu.vote.model;

import java.util.ArrayList;
import java.util.Date; 
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdGeneratorStrategy; 
import javax.jdo.annotations.IdentityType; 
import javax.jdo.annotations.PersistenceCapable; 
import javax.jdo.annotations.Persistent; 
import javax.jdo.annotations.PrimaryKey; 

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.users.User; 
import com.nwpu.vote.control.VotePersistentImpl;
import com.nwpu.vote.util.PMF;
import com.nwpu.vote.util.PersistentException;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Vote {
	// content mapping ------------------------------------------------
	@PrimaryKey 
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) 
    private Key key; 
 
    @Persistent 
    private Key creatorKey;
    
    @Persistent 
    private String department;
        
    @Persistent 
    private String title;
 
    @Persistent 
    private Date createDate;
    
    @Persistent 
    private Date startDate;
    
    @Persistent 
    private Date finishDate;
    
    @Persistent 
    private String description;
 
    @Persistent 
    private Set<Key> tags = new HashSet<Key>();
    
    @Persistent 
    private Set<Key> votedUsersKeys = new HashSet<Key>();
    
    public Vote(Key creatorKey, String department, String title, Date createDate,
    		Date startDate, Date finishDate, String description) { 
        this.creatorKey = creatorKey; 
        this.department = department;
        this.title = title;
        this.createDate = createDate; 
        this.startDate = startDate;
        this.finishDate = finishDate;
        this.description = description;
    } 
 
    // get and set ----------------------------------------------------
    public Key getKey() {
		return key;
	} 
 
    public Key getCreatorKey() { 
        return creatorKey; 
    } 
 
    public String getDepartment() { 
        return department; 
    } 
 
    public String getTitle() { 
        return title; 
    }
    
    public Date getCreateDate() { 
        return createDate; 
    } 
    
    public Date getStartDate() { 
        return startDate; 
    } 
 
    public Date getFinishDate() { 
        return finishDate; 
    }
    
    public String getDescription() {
    	return description;
    }
    
    public Set<Key> getTags() {
		return tags;
	}
    
    public Set<Key> getVotedUsersKeys() {
		return votedUsersKeys;
	}
    
    public void setCreatorKey(Key creatorKey) { 
        this.creatorKey = creatorKey; 
    } 
 
    public void setDepartment(String department) { 
        this.department = department; 
    } 
    
    public void setTitle(String title) { 
        this.title = title; 
    } 
 
    public void setCreateDate(Date createDate) { 
        this.createDate = createDate; 
    } 

    public void setStartDate(Date startDate) { 
        this.startDate = startDate; 
    } 
    
    public void setFinishDate(Date finishDate) { 
        this.finishDate = finishDate; 
    } 
    
    public void setDescription(String description) {
    	this.description = description;
    }
    
    public void addTag(Tag tag){
    	this.tags.add(tag.getKey());
    }
    
    public String displayTags(){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		StringBuilder sb = new StringBuilder();
		
		if(this.tags != null){
			for(Key key: this.tags){
				Tag tag = pm.getObjectById(Tag.class,key);
				sb.append(tag.getName()+" ");
			}
		}
		
		return sb.toString();
	}
    
    public String displayTagsInHtml(){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		StringBuilder sb = new StringBuilder();
		
		if(this.tags != null){
			for(Key key: this.tags){
				Tag tag = pm.getObjectById(Tag.class,key);
				sb.append(tag.getName()+"<br/>");
			}
		}
		
		return sb.toString();
	}
    
    public void addVotedUsers(VoteUser user){
    	this.votedUsersKeys.add(user.getKey());
    }
    
    public List<VoteUser> getVotedUsers(){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<VoteUser> users = new ArrayList<VoteUser>();
		
		if(this.votedUsersKeys != null){
			for(Key key: this.votedUsersKeys){
				VoteUser user = pm.getObjectById(VoteUser.class, key);
				users.add(user);
			}
		}
		
		return users;
	}
    
    public String displayVotedUsers(){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		StringBuilder sb = new StringBuilder();
		
		if(this.votedUsersKeys != null){
			for(Key key: this.votedUsersKeys){
				VoteUser user = pm.getObjectById(VoteUser.class, key);
				sb.append(user.getName()+" ");
			}
		}
		
		return sb.toString();
	}
    
    public String displayVotedUsersInHtml(){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		StringBuilder sb = new StringBuilder();
		
		if(this.votedUsersKeys != null){
			for(Key key: this.votedUsersKeys){
				VoteUser user = pm.getObjectById(VoteUser.class, key);
				sb.append(user.getName()+"<br/>");
			}
		}
		
		return sb.toString();
	}
}
